<?php defined( '_VALID_MOS' ) or die( include("404.php") );
class core_class{
	// ham kiem tra su hop le cua file(kiem tra file co ton tai)
	function _routers( $file_path ){
		if( file_exists( $file_path ) ){
			return true;
		} else {
			return false;
		}
	}
	
	// ham nay tra ve tap hop mang la cac gia tri trong tung cap the / cua URL
	function _extract_url( $uri ){
		$url = strip_tags( $uri );
		$url_array = explode(",", $url);
		// loai bo 1 gia tri dau trong URI
		//array_shift($url_array);
		return $url_array; 
	}
		
	/* ham chuyen trang */
	function _redirect( $url ){
		/*kiem tra neu header already by send thi chuyen trang = script nguoc lai chuyen trang = code server*/
		if (headers_sent()) {
			echo "<script>document.location.href='$url';</script>\n";
		} else {
			ob_end_clean(); // clear output buffer
			header( 'HTTP/1.1 301 Moved Permanently' );
			header( "Location: $url" );
		}
		exit();
	}
	// ham dinh dang kieu ngay thang
	function _formatdatetime( $date ){
		return substr($date, -4) ."/". substr($date, 3, 2) ."/". substr($date, 0, 2) . " ".  date('h:i:s');
	}
	// ham go bo dau cua 1 chuoi
	function _removesigns($text){
		//global $ibforums;<BR>//Charachters must be in ASCII and certain ones aint allowed
		$text = html_entity_decode ($text);
		$text = preg_replace('/(ä|à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/', 'a', $text);
		$text = str_replace('ç','c',$text);
		$text = preg_replace('/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/', 'e', $text);
		$text = preg_replace('/(ì|í|î|ị|ỉ|ĩ)/', 'i', $text);
		$text = preg_replace('/(ö|ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/', 'o', $text);
		$text = preg_replace('/(ü|ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/', 'u', $text);
		$text = preg_replace('/(ỳ|ý|ỵ|ỷ|ỹ)/', 'y', $text);
		$text = preg_replace('/(đ)/', 'd', $text);
		//CHU HOA
		$text = preg_replace('/(Ä|À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/', 'A', $text);
		$text = str_replace('Ç','C',$text);
		$text = preg_replace('/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/', 'E', $text);
		$text = preg_replace('/(Ì|Í|Ị|Ỉ|Ĩ)/', 'I', $text);
		$text = preg_replace('/(Ö|Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/', 'O', $text);
		$text = preg_replace('/(Ü|Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/', 'U', $text);
		$text = preg_replace('/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/', 'Y', $text);
		$text = preg_replace('/(Đ)/', 'D', $text);
		//Special string
		/*
		$text = preg_replace('/( |!|”|#|$|%|’)/', ', $text);
		$text = preg_replace('/(̀|́|̉|$|&gt;)/', ', $text);
		$text = preg_replace (''&lt;[\/\!]*?[^&lt;&gt;]*?&gt;'si', '', $text);
		*/
		
		$text = str_replace(' / ','-',$text);
		$text = str_replace('/','-',$text);
		$text = str_replace(' - ','-',$text);
		$text = str_replace('_','-',$text);
		$text = str_replace(' ','-',$text);
		$text = str_replace( 'ß', 'ss', $text);
		$text = str_replace( '&amp;', '', $text);
		$text = str_replace( '%', '', $text);
		$text = ereg_replace('[^A-Za-z0-9-]', '', $text);
		
		/*$text = str_replace('—-','-',$text);
		$text = str_replace('—','-',$text);
		$text = str_replace('–','-',$text);*/
		return $text;
	}
	
	function _getmetadata(){
		include("protected/dbconnect.php");
		$sql = "SELECT `menu`.`title`, `menu`.`active_title`, `menu`.`active_date` FROM `menu` where `Id` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $_GET["item"]);
		$cmd->execute() or die($mysqli->error);
		$cmd->bind_result( $title, $active_title, $active_date );
		$cmd->store_result();
		if($cmd->fetch()){
			$ret = new stdClass;
			$ret->title = $title;
			$ret->title_is_active = $active_title;
			$ret->date_is_active = $active_date;
		}
		return $ret;
	}
	
function _CSP_loadmenu($parent_Id='', $menu_type_id='', $icon=''){
		include("protected/dbconnect.php");
		$sql = "SELECT `Id`, CONCAT(`link` , `link_id`) as `link`, `title`, `type`, `target` FROM `menu` where parent_Id = $parent_Id AND `menu`.`menu_type_id` = $menu_type_id  AND `activated` = 1 order by order_num";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->store_result();
		$cmd->bind_result($id, $link, $title, $type, $target);
		$i = 0;
		while($cmd->fetch()){                    
                    if(isset($_GET["item"])){
				if($id == $_GET["item"]){ 
					if($type == 'linkout'){ 
						$Menu .= "<li> $icon <a target=\"$target\" href=\"$link\">$title</a>";                                                                                                
						$Menu .= "<ul class='children'>";
                        $Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                        $Menu .= "</ul>";
						$Menu .= '</li>';
                                                
					} else {
						$Menu .= "<li> $icon <a target=\"$target\" href=\"$link/$id.csp\">$title</a>";
                                                $Menu .= "<ul class='children'>";
						$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                                                $Menu .= "</ul>";
						$Menu .= '</li>';
					}
				} else {
					if($type == 'linkout'){
						$Menu .= "<li> $icon <a target=\"$target\" href=\"$link\">$title</a>";
                                                $Menu .= "<ul class='children'>";
						$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                                                $Menu .= "</ul>";
						$Menu .= '</li>';
					} else {
						$Menu .= "<li> $icon <a target=\"$target\" href=\"$link/$id.csp\">$title</a>";
                                                $Menu .="<ul class='children'>";
						$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
						$Menu .= '</ul></li>';
					}
				}
			} else {
				if($type == 'linkout'){
					if($i == 0){
						$Menu .= "<li> $icon <a target=\"$target\" href=\"$link\">$title</a>";
                                                $Menu .="<ul class='children'>";
						$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
						$Menu .= '</ul></li>';
					} else {
						$Menu .= "<li> $icon <a target=\"$target\" href=\"$link\">$title</a>";
                                                $Menu .="<ul class='children'>";
						$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
						$Menu .= '</ul></li>';
					}
				} else {
                                        
					$Menu .= "<li> $icon <a target=\"$target\" href=\"$link/$id.csp\">$title</a>";
                                        $Menu .="<ul class='children'>";
					$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
					$Menu .= '</ul></li>';
				}
			}
			$i++;
                }
		return $Menu;                           
        }
        
function _CSP_loadmenu_ver($parent_Id='', $menu_type_id='', $icon=''){
	include("protected/dbconnect.php");
	$sql = "SELECT `Id`, CONCAT(`link` , `link_id`) as `link`, `title`, `type`, `target` FROM `menu` where parent_Id = $parent_Id AND `menu`.`menu_type_id` = $menu_type_id  AND `activated` = 1 order by order_num";
	$cmd2 = $mysqli->prepare($sql);
	$cmd2->execute();
	$cmd2->store_result();
	$cmd2->bind_result($id, $link, $title, $type, $target);
	$i = 0;
	while($cmd2->fetch()){                    
            if(isset($_GET["item"])){
		if($id == $_GET["item"]){ 
                    if($type == 'linkout'){                         
			$Menu .= "<li ='initialExpandedMenuItem'> $icon <a target=\"$target\" href=\"$link\">$title</a>";
                        $Menu .= "<ul>";
                        $Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                        $Menu .= "</ul>";
			$Menu .= '</li>';
                                                
                    } else { 

                            $Menu .= "<li ='initialExpandedMenuItem'> $icon <a target=\"$target\" href=\"$link/$id.csp\">$title</a>";                            
                            $Menu .= "<ul>";
                            $Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                            $Menu .= "</ul>";
                            $Menu .= '</li>';
                    }
		} else {
                    if($type == 'linkout'){
			$Menu .= "<li ='initialExpandedMenuItem'> $icon <a target=\"$target\" href=\"$link\">$title</a>";                        
                        $Menu .= "<ul>";
			$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                        $Menu .= "</ul>";
                        $Menu .= "</li>";
                                                
                    } else {
			$Menu .= "<li ='initialExpandedMenuItem'> $icon <a target=\"$target\" href=\"$link/$id.csp\">$title</a>";                        
                        $Menu .= "<ul>";
			$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                        $Menu .= "</ul>";
                        $Menu .="</li>";
                    }
		}
            } else {
		if($type == 'linkout'){
                    if($i == 0){
                        $Menu .= "<li ='initialExpandedMenuItem'> $icon <a target=\"$target\" href=\"$link\">$title</a>";
                        $Menu .= "<ul>";
			$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                        $Menu .= "</ul>";
                        $Menu .="</li>";
                    } else {
			$Menu .= "<li ='initialExpandedMenuItem'> $icon <a target=\"$target\" href=\"$link\">$title</a>";
                        $Menu .= "<ul>";
			$Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                        $Menu .= "</ul>";
                        $Menu .="</li>";
                    }
		} else {
                    $Menu .= "<li ='initialExpandedMenuItem'> $icon <a target=\"$target\" href=\"$link/$id.csp\">$title</a>";
                    $Menu .= "<ul>";
                    $Menu .= core_class::_CSP_loadsubmemu($id, $menu_type_id, $icon);
                    $Menu .= "</ul>";
                    $Menu .="</li>";
		}
            }
	$i++;
        }
	return $Menu;                           
}				
	function _CSP_loadsubmemu($id='', $menu_type_id='', $icon=''){
		include("protected/dbconnect.php");
		$sql1 = "SELECT `Id`, CONCAT(`link` , `link_id`) as `link`, `title`, `type`, `target` FROM `menu` where parent_Id = $id AND `menu`.`menu_type_id` = $menu_type_id AND `activated` = 1  order by order_num";
		$cmd1 = $mysqli->prepare($sql1);
		$cmd1->execute();
		$cmd1->store_result();
		$cmd1->bind_result($sub_Id, $sub_link, $sub_title, $sub_type, $sub_target);
		if($cmd1->num_rows > 0){			
			while($cmd1->fetch()){
				if($sub_type == 'linkout'){
					$Sub_Menu .= "<li><a target=\"$sub_target\" href=\"$sub_link\">$sub_title</a>";
                    $Sub_Menu .="<ul>";                                        
					$Sub_Menu .= core_class::_CSP_loadsubmemu($sub_Id, $menu_type_id, $icon);
                    $Sub_Menu .="</ul>";
					$Sub_Menu .= "</li>";
				} else {
					$Sub_Menu .= "<li><a target=\"$sub_target\" href=\"$sub_link/$sub_Id.csp\">$sub_title</a>";
                    $Sub_Menu .="<ul>";                                        
					$Sub_Menu .= core_class::_CSP_loadsubmemu($sub_Id, $menu_type_id, $icon);
                    $Sub_Menu .="</ul>";
					$Sub_Menu .= "</li>";
                                        
				}
			}
		}
		return $Sub_Menu;
	}
	function SmartContent( $text, $length=200 ) {
		// strips tags won't remove the actual jscript
		$text = preg_replace( "'<script[^>]*>.*?</script>'si", "", $text );
		$text = preg_replace( '/{.+?}/', '', $text);
	
		$text = preg_replace( '/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is','\2', $text );
	
		// replace line breaking tags with whitespace
		$text = preg_replace( "'<(br[^/>]*?/|hr[^/>]*?/|/(div|h[1-6]|li|p|td))>'si", ' ', $text );
		
		$text = core_class::SmartSubstr( strip_tags( $text ), $length );
	
		return $text;
	}
	
	function SmartSubstr($text, $length=200) {
		$strlength = strlen($text);
		if ($strlength > $length) {
			$str = substr($text, 0, $length);
			$text = substr( $str, 0, strrpos($str, " ") );
			return $text . " ...";
		} else {
			return $text;
		}
	}
	
	function _counterblock( $name ){
		include("protected/dbconnect.php");
		$sql = "SELECT `block`.`Id`, `block`.`title`, `block`.`block_path`
				FROM `group_block` INNER JOIN `block` ON `group_block`.`Id` = `block`.`block_group_Id`
				WHERE  `group_block`.`activated` = 1 AND `block`.`activated` = 1 AND `group_block`.`location` = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $name);
		$cmd->execute();
		$cmd->bind_result($Id, $title, $block_path);
		$cmd->store_result();
		while($cmd->fetch()){
			echo "<div>
					<div id=box_title>$title</div>
					<div id=box_body>
						<div id=box_body_content>
							<div id=content>";
								include_once($block_path);
								"<div class=line></div>
								<div class=clear></div>
							</div>
						</div>						
					</div>
				</div>";
		}
	}	
}
?>

